package com.app.android.coliy.util;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;

import com.app.android.coliy.R;

public class SoundUtil {

    private volatile static SoundUtil instance;

    private SoundUtil() {}

    public static SoundUtil getInstance() {
        if (instance == null) {                         //Single Checked
            synchronized (SoundUtil.class) {
                if (instance == null) {                 //Double Checked
                    instance = new SoundUtil();
                }
            }
        }

        instance.createSoundPool();

        return instance ;
    }


    private SoundPool mSoundPool = null;

    private void createSoundPool() {
        SoundPool.Builder builder = new SoundPool.Builder();
        //传入最多播放音频数量
        builder.setMaxStreams(1);
        //AudioAttributes是一个封装音频各种属性的方法
        AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
        //设置音频流的合适的属性
        attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
        //加载一个AudioAttributes
        builder.setAudioAttributes(attrBuilder.build());
        mSoundPool = builder.build();
    }


    public void playTransferFileSound(Context context) {

        if (mSoundPool == null) {
            createSoundPool();
        }

        final int voiceId = mSoundPool.load(context, R.raw.sound_transfer_file, 1);

        mSoundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
            if (status == 0) {
                mSoundPool.play(voiceId,1,1,1,0,1);
            }
        });
    }

    public void playTransferFileEnSound(Context context) {

        if (mSoundPool == null) {
            createSoundPool();
        }

        final int voiceId = mSoundPool.load(context, R.raw.sound_transfer_file_en, 1);

        mSoundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
            if (status == 0) {
                mSoundPool.play(voiceId,1,1,1,0,1);
            }
        });
    }
}
